package com.ccx.coachsite.ui.manage;

import java.util.ArrayList;
import java.util.List;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.Toast;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.TextView;

import com.ccx.coachsite.R;
import com.ccx.coachsite.bus.CustomInfo;
import com.ccx.coachsite.bus.ManageInfo;
import com.ccx.coachsite.ui.login.TopViewUtil;
import com.ccx.coachsite.ui.own.OwerBaseView;
import com.ccx.coachsite.ui.own.OwerMainBase;

public class ManageCustomView extends OwerBaseView {
	private ManageInfo mApp;

	private ManageCustomGroupAdapter mGroupAdapter;
	private ExpandableListView mExpandListView;
	List<CustomInfo> inviteList;
	List<CustomInfo> uninviteList;
	private boolean isSort;
	private Integer viewType;
	
	public ManageCustomView(Context context, OwerMainBase superView, Integer vt) {
		super(context, superView);
		loadData();

		isSort = false;
		viewType = vt;
		if(viewType == null)
		{
			viewType = 3;
		}
		mExpandListView = (ExpandableListView)findViewById(R.id.custom_group_list);
		mExpandListView.setOnGroupCollapseListener(new OnGroupCollapseListener()
		{

			@Override
			public void onGroupCollapse(int groupPosition) {
				// TODO Auto-generated method stub
				mExpandListView.expandGroup(groupPosition);
			}
			
		});
		mExpandListView.setOnGroupClickListener(new OnGroupClickListener()
		{
		    @Override
		    public boolean onGroupClick(ExpandableListView parent, 
		        View v, int groupPosition, long id)
		    {
		        return false;
		    }
		});
	}
	
	public String getTitle()
	{
		if(viewType == 2)
			return "邀请客户";
		else if(viewType == 3)
		{
			TopViewUtil.setRightVisable((Activity)mContext, R.id.login_top_right, View.INVISIBLE);
			return "消息列表";
		}
		
		
		
		return "";
	}
	
	public void sortCustom()
	{
		
		if(viewType == 1 && inviteList != null && inviteList.size() == 0)
			return;
		
		if(viewType == 2 && uninviteList != null && uninviteList.size() == 0)
			return;
		
		new AlertDialog.Builder(mContext).setTitle("排序方式").setItems(
				new String[] { "入网时间", "基准值" },
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						if (which == 0) {
							sortCustomColumn(0);
						} else {
							sortCustomColumn(1);
						}
					}
				}).show();
	}
	
	public void sortCustomColumn(int index)
	{
		if(isBusy)
			return;
		
		CustomInfo.SortType = 1;
		if(index == 0)
			CustomInfo.SortType = 1;
		else if(index == 1)
			CustomInfo.SortType = 2;
		isSort = true;
		loadData();
	}

	protected boolean onLoadData() {
		mApp = ManageProvider.getInstance().getManageInfo(mContext);
		if (mApp == null)
		{
			return false;
		}
			
		return true;
	}

	protected void doRefreshDataSuccess() {
		
		TextView date1 = (TextView)findViewById(R.id.manage_custom_info_add_count);
		date1.setText(mApp.getHasClaimedNum() + "");
		
		TextView date2 = (TextView)findViewById(R.id.manage_custom_info_last_count);
		date2.setText(mApp.getStillCanClaim() + "");
		
		inviteList = mApp.getInviteCustoms(true);
		if (inviteList == null)
			inviteList = new ArrayList<CustomInfo>();
		
		uninviteList = mApp.getInviteCustoms(false);
		if (uninviteList == null)
			uninviteList = new ArrayList<CustomInfo>();
		
		if(viewType == 1)
		{
			if (uninviteList.size() > 0)
			{
				inviteList.addAll(uninviteList);
			}
		}
		
		if(uninviteList.size() > 0)
		{
			CustomInfo cinfo = new  CustomInfo();
			cinfo.setId(-1);
			uninviteList.add(0, cinfo);
		}
		
		
		mGroupAdapter = new ManageCustomGroupAdapter(mContext, inviteList, uninviteList, this, viewType);
		mExpandListView.setChildDivider(mContext.getResources().getDrawable(R.drawable.ower_sip_line));
		mExpandListView.setAdapter(mGroupAdapter);
		mExpandListView.setGroupIndicator(null);
		int groupCount = mGroupAdapter.getGroupCount();
		
		
		for (int i=0; i<groupCount; i++) {
			mExpandListView.expandGroup(i);
		}
		
		mExpandListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                public boolean onChildClick(ExpandableListView parent,
                                View v, int groupPosition, int childPosition,
                                long id) {
                	return listItemClick(v, groupPosition, childPosition);
                }
        });
		
		if(isSort)
		{
			isSort = false;
			String str = "您已按入网时间对客户进行排序";
			if(CustomInfo.SortType == 2)
				str = "您已按基准值对客户进行排序";
			Toast.makeText(mContext, str, Toast.LENGTH_SHORT).show();
		}
	}
	
	private boolean listItemClick(View v, int groupPosition, int childPosition)
	{
		if(viewType  == 1)
		{
			SwitchView(ManageKey.ll_manage_custom_info, inviteList.get(childPosition));
		}
    	else if(viewType  == 2)
		{
			if(childPosition == 0)
				return false;
			SwitchView(ManageKey.ll_manage_custom_info, uninviteList.get(childPosition));
		}
    	else if(viewType  == 3)
		{
    		TextView MessageCount = (TextView) v.findViewById(R.id.text_custom_message_count);
    		CustomInfo cinfo = inviteList.get(childPosition);
			cinfo.setMessageBtn(MessageCount);
			SwitchView(ManageKey.ll_manage_custom_message, cinfo);
		}
    	return true;
	}

}

